package com.yuan.yblog.search.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yuan.yblog.common.api.CommonResult;
import com.yuan.yblog.search.pojo.EsPostDTO;
import com.yuan.yblog.search.service.EsPostService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author hepeiyuan on 2024/5/26 21:16
 * @description 文章搜索控制层
 */
@Tag(name = "文章搜索接口")
@RequestMapping("/esPost")
@RestController
public class EsPostController {

    @Autowired
    EsPostService esPostService;

    @Operation(summary = "根据关键词，匹配指定字段，分页得到搜索结果")
    @GetMapping("/search/keyword")
    public CommonResult<IPage<EsPostDTO>> search(
            @Parameter(description = "关键词") @RequestParam String keyword,
            @Parameter(description = "字段（标题or内容）") @NotEmpty @RequestParam String filed,
            @Parameter(description = "是否精确搜索（true精确，false模糊）") @RequestParam(defaultValue = "false") Boolean isPrecise,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") int pageNumber,
            @Parameter(description = "页大小") @RequestParam(defaultValue = "10") int pageSize,
            @Parameter(description = "排序字段") @RequestParam(defaultValue = "created") String sortKey,
            @Parameter(description = "是否降序") @RequestParam(defaultValue = "true") Boolean isDesc) {

        IPage<EsPostDTO> result = esPostService.search(keyword, filed, isPrecise, pageNumber-1, pageSize, sortKey, isDesc);
        return CommonResult.success(result);
    }

    @Operation(summary = "根据文章id，推荐相关文章（未完成）")
    @GetMapping("/search/recommend")
    public CommonResult searchRecommend(
            @Parameter(description = "文章id") @RequestParam Long postId,
            @Parameter(description = "页码") @RequestParam(required = false, defaultValue = "1") int pageNumber,
            @Parameter(description = "页大小") @RequestParam(required = false, defaultValue = "10") int pageSize) {
        return CommonResult.failed();
    }
}
